package ServerStuff;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Utilities.EmailClient.EmailHeader;

import Database.DatabaseConnector;

@WebServlet("/listAllBusinessesServlet")
public class listAllBusinessesServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		DatabaseConnector dbConnect = new DatabaseConnector();
		ArrayList<String> businesses = dbConnect.getAllBusNames();
		
		JSONObject result = new JSONObject();
		
		JSONArray jArray = new JSONArray();
		
		try {
			for(String business : businesses) {
				jArray.put(business);
			}
		
			result.put("businesses", jArray);
			
			response.getWriter().print(result.toString());
			response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		}
		catch (JSONException e) {
			e.printStackTrace();
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
	}
}
